package fast.loader;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import fast.data.CharacterClass;

public class TalentDB {
	private CharacterClass characterClass;
	
	private TreeMap<Integer,TalentTree> talentMap2 = new TreeMap<Integer,TalentTree>();
	//private HashMap<Integer,Glyph> glyphMap = new HashMap<Integer,Glyph>();
	
	public void populateTalents() {
		
		File file = new File("./static/talents.json");
		String json = null;
		if(file.exists()){
			BufferedReader in;
			try {
				in = (new BufferedReader(new FileReader(file)));
				json = in.readLine();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else{
			json = FastLoader.loadURL("http://us.battle.net/api/wow/data/talents");
			File dbDir = new File("./static/");
			if(!dbDir.exists())
				dbDir.mkdir();
			PrintWriter out;
			try {
				out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
				out.write(json);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		JSONObject js = null;
		try {
			js = new JSONObject(json);
			//System.out.println(js.toString(1));
			
			for ( Iterator<?> i = js.keys(); i.hasNext(); )
			{
				String s = (String) i.next();
				JSONObject charclass = js.getJSONObject(s);
				JSONArray talents = charclass.getJSONArray("talents");
				//System.out.println(talents.toString(1));
				
				//System.out.println(talents.getJSONArray(0).toString(1));
				TalentTree ttree = new TalentTree();
				for(int j = 0; j < talents.length(); j++) {
					JSONArray tier = talents.getJSONArray(j);
					TalentTier tt = new TalentTier();
					for(int k = 0; k < tier.length(); k++) {
						JSONObject column = tier.getJSONObject(k);
						//System.out.println(column.getJSONObject("spell").getString("name"));
						tt.column[k] = formatTalentName(column.getJSONObject("spell").getString("name"));
						//System.out.println(formatTalentName(tt.column[k]));
					}
					ttree.tier[j] = tt;
				}
				talentMap2.put(Integer.parseInt(s), ttree);
			}
			
			/*for ( Iterator<?> i = js.keys(); i.hasNext(); )
			{
				String s = (String) i.next();
				JSONObject charclass = js.getJSONObject(s);
				JSONArray glyphs = charclass.getJSONArray("glyphs");
				for(int j = 0; j < glyphs.length(); j++) {
					JSONObject glyph = glyphs.getJSONObject(j);
					int glyphId = glyph.getInt("glyph");
					String glyphName = formatGlyphName(glyph.getString("name"));
					String glyphType = glyph.getInt("typeId") == 0 ? "prime" : "major";
					Glyph g = new Glyph(glyphName, glyphType);
					glyphMap.put(glyph.getInt("glyph"), g);
					System.out.println("glyphDB.put("+glyphId+", new Glyph(\""+glyphName+"\",\""+glyphType+"\"));");
				}
				System.out.println("// ");
			}*/
			
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
	}
	
	public TalentDB(CharacterClass cc){
		characterClass = cc;
		populateTalents();
	}
	
	public String get(int i,int j){
		if(i > 6 || i < 0 || j > 3 || j < 0)
			return "";
		return talentMap2.get(characterClass.getId()).tier[i].column[j];
	}
	
	@SuppressWarnings("unused")
	private String formatGlyphName(String name){
		name = name.replace("Glyph of ", "");
		if(name.startsWith("the "))
			name = name.replace("the ", "");
		name = name.toLowerCase();
		name = name.replace(" ", "_");
		return name;
	}
	
	private String formatTalentName(String name){
		name = name.replace("'", "");
		name = name.replace(",", "");
		name = name.toLowerCase();
		name = name.replace(" ", "_");
		name = name.replace("-", "_");
		return name;
	}
	
	class TalentTree {
		TalentTier[] tier = new TalentTier[6];
	}
	
	class TalentTier {
		String[] column = new String[3];
	}
}
